package com.yuejian.neusoft.yuejian_24.model;

import com.yuejian.neusoft.yuejian_24.javaBean.Login;
import com.yuejian.neusoft.yuejian_24.javaBean.Success;
import com.yuejian.neusoft.yuejian_24.javaBean.User;
import com.yuejian.neusoft.yuejian_24.listener.Loginlistener;
import com.yuejian.neusoft.yuejian_24.listener.SuccessListener;
import com.yuejian.neusoft.yuejian_24.listener.UserListener;
import com.yuejian.neusoft.yuejian_24.service.UserService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by q4549 on 2017/9/28.
 */
//http://172.19.86.103/yuejian/
public class UserModel {
    private Retrofit retrofit;
    private static final String BASE_URL = "http://10.0.2.2/yuejian/";//主机地址
    public UserModel(){
        //Retrofit使用-1
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)               //主机地址
                .addConverterFactory(GsonConverterFactory.create()) //解析方式
                .build();
    }

   public void login(String phone, String pwd, final Loginlistener listener){
       UserService userService = retrofit.create(UserService.class);
       Call call = userService.login(phone,pwd);
       Callback<Login> callback = new Callback<Login>() {
           @Override
           public void onResponse(Call<Login> call, Response<Login> response) {
               listener.onResponse(response.body());
           }

           @Override
           public void onFailure(Call<Login> call, Throwable t) {

           }
       };
       call.enqueue(callback);
   }
    public void reg(String phone, String username, String pwd, final SuccessListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.reg(phone,username,pwd);
        Callback<Success> callback = new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<Success> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }
    public void getuserinfo(String uid, final UserListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.getuserinfo(uid);
        Callback<User> callback = new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }
}
